package com.framework.Utils;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则匹配util
 *
 * @author Yangjie
 *         className RegularUtil
 *         created at  2017/4/12  10:42
 */
public class RegularUtil {

    private static volatile RegularUtil singleton;

    private RegularUtil() {
    }

    public static RegularUtil getInstance() {
        if (singleton == null) {
            synchronized (RegularUtil.class) {
                if (singleton == null) {
                    singleton = new RegularUtil();
                }
            }
        }
        return singleton;
    }

    /**
     * 匹配邮箱
     *
     * @param email email
     * @return boolean
     */
    public boolean isEmail(String email) {
        if (TextUtils.isEmpty(email)) {
            return false;
        }
        String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证号码的有效性
     *
     * @param mobile mobile
     * @return boolean
     */
    public boolean isMobileNO(String mobile) {
        if (TextUtils.isEmpty(mobile)) {
            return false;
        }
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,1,2,5-9])|(17[0-9]))\\d{8}$");
        Matcher m = p.matcher(mobile);
        return m.matches();
    }

    /**
     * 验证数字
     *
     * @param number number
     * @return boolean
     */
    public boolean isNumberAtLeast6bit(String number) {
        if (TextUtils.isEmpty(number)) {
            return false;
        }
        Pattern p = Pattern
                .compile("^[0-9]{6,}$");
        Matcher m = p.matcher(number);
        return m.matches();
    }

    public boolean isNumberAtLeast1bit(String number) {
        if (TextUtils.isEmpty(number)) {
            return false;
        }
        Pattern p = Pattern
                .compile("^[0-9]+$");
        Matcher m = p.matcher(number);
        return m.matches();
    }

    /**
     * 验证数字字母
     *
     * @param str str
     * @return boolean
     */
    public boolean isCharOrNumber(String str) {
        Pattern p = Pattern
                .compile("^[_a-zA-Z0-9]{6,}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }
}